package top.hkyzf.study.designpattern.singleton;

/**
 * 懒汉式
 * 用到了在进行初始化
 */
public class Singleton5 {
    // 禁止指令重排序
    private static volatile Singleton5 instance;

    private Singleton5() {

    }

    // DCL (Double Check Lock 双端检锁机制)
    public static Singleton5 getInstance() {
        if(instance == null) {
            synchronized (Singleton5.class) {
                if (instance == null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    instance = new Singleton5();
                }
            }
        }
        return instance;
    }
}
